﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LumenLiquid.Database;
using LumenLiquid.Database.Entities.World;
using LumenLiquid.Debug;
using LumenLiquid.InGame.world;
using Microsoft.Xna.Framework;
using NHibernate;
using DebugLevel = LumenLiquid.Debug.DebugHandler.EDebugLevel;

namespace LumenLiquid.InGame
{
    public class World
    {
        private enum EWorldProperties
        {
            StartupTeleporter = 1
        }

        private ISessionFactory _worldDatabase;
        private Properties _properties;
        private Map _map;

        public bool Init(int worldId)
        {
            DebugHandler.Log("Initializing world with id \"" + worldId + "\"", DebugLevel.Code);

            SIdInfo worldInfo = MainGame.Instance.GetWorlds().FirstOrDefault(m => m.Id.Equals(worldId));

            if (worldInfo.Id == -1)
            {
                DebugHandler.Log("World id \"" + worldId + "\" not found in the database",
                                 DebugLevel.Error);
                return false;
            }

            var worldFolder = worldInfo.Param1;

            _worldDatabase = SessionFactory.CreateWorldSessionFactory(worldFolder);



            _properties = new Properties(_worldDatabase);
            _properties.LoadSettings();

            //ToDo: remove later
            if (LoadStartupMap() == false)
                return false;

            DebugHandler.Log("World with id \"" + worldId + "\" sucessfully loaded", DebugLevel.Code);

            return true;
        }

        public void ShutdownTransactions()
        {
            DebugHandler.Log("Unloading world...");
            _properties.DisposeSettings(true);
        }

        private bool LoadStartupMap()
        {
            var session = _worldDatabase.OpenSession();
            var teleId = Convert.ToInt32(_properties[(int)EWorldProperties.StartupTeleporter]);

            var teleporter = DatabaseInteraction.ReadObject<Teleporter>(session, teleId);

            if (teleporter == null)
            {
                DebugHandler.Log("Startup teleporter with id \"" + teleId + "\" not found in database.", DebugLevel.Error);
                return false;
            }
            DebugHandler.Log("Loaded startup teleporter with id \"" + teleId + "\" sucessfully");

            LoadMap(teleporter.MapId);

            session.Close();
            return true;
        }

        private void LoadMap(int id)
        {
            if (_map != null)
                _map.ShutdownTransactions();
            _map = new Map();
            _map.Init(id, _worldDatabase);
        }

        #region XNA Functions
        public void Draw(GameTime gameTime)
        {
            _map.Draw(gameTime);
        }

        public void Update(GameTime gameTime)
        {
            _map.Update(gameTime);
        }
        #endregion
    }
}
